package id.net.iconpln.bb.client.view;

import id.net.iconpln.bb.client.service.TransportRPCService;
import id.net.iconpln.bb.client.service.TransportRPCServiceAsync;
import id.net.iconpln.bb.client.util.ClientModule;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.extjs.gxt.ui.client.Registry;
import com.extjs.gxt.ui.client.Style.Orientation;
import com.extjs.gxt.ui.client.Style.SelectionMode;
import com.extjs.gxt.ui.client.core.FastMap;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.store.GroupingStore;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.extjs.gxt.ui.client.widget.grid.Grid;
import com.extjs.gxt.ui.client.widget.grid.GridGroupRenderer;
import com.extjs.gxt.ui.client.widget.grid.GroupColumnData;
import com.extjs.gxt.ui.client.widget.grid.GroupingView;
import com.extjs.gxt.ui.client.widget.layout.FitData;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.extjs.gxt.ui.client.widget.layout.RowData;
import com.extjs.gxt.ui.client.widget.layout.RowLayout;
import com.extjs.gxt.ui.client.widget.toolbar.LabelToolItem;
import com.extjs.gxt.ui.client.widget.toolbar.SeparatorToolItem;
import com.extjs.gxt.ui.client.widget.toolbar.ToolBar;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.rpc.AsyncCallback;

public class PageMonitoringPemakaian extends LayoutContainer {
	private final TransportRPCServiceAsync service = GWT.create(TransportRPCService.class);
	private Grid grid;
	private ListStore storeResult;
	private GroupingStore storePemakaian;
	private Button btnRefresh;
	
	@Override
	  protected void onRender(Element parent, int pos) {
	    super.onRender(parent, pos); 
	    setLayout(new FitLayout()); 
	    
	    ContentPanel cp = new ContentPanel();  
	  
	    cp.setHeading("Daftar Pemakaian");  
	    cp.setFrame(true);  
	    cp.setLayout(new RowLayout(Orientation.HORIZONTAL));  
	  
	    ContentPanel cpShip = new ContentPanel();
	    cpShip.setLayout(new FitLayout());
	    cpShip.setHeaderVisible(false);
	    
	    grid = createGrid();
	    grid.getSelectionModel().setSelectionMode(SelectionMode.SINGLE);	
	    ToolBar tbJadwal = new ToolBar();
	    tbJadwal.add(new LabelToolItem("<b>Data Pemakaian</b>"));
	    tbJadwal.add(new SeparatorToolItem());
	    
	    btnRefresh = new Button("Refresh");
	    tbJadwal.add(btnRefresh);
	    tbJadwal.add(new SeparatorToolItem());	    
	    
	    cpShip.setTopComponent(tbJadwal);
	    cpShip.add(grid);
	    
	    cp.add(cpShip, new RowData(.6, 1)); 
	    add(cp, new FitData(5)); 
	    
	    getList();
	    
	    btnRefresh.addSelectionListener(new SelectionListener<ButtonEvent>() {
			
			@Override
			public void componentSelected(ButtonEvent ce) {
				// TODO Auto-generated method stub
				getList();
			}
		});
	  }  
	  
	  
	  
	  private Grid createGrid() {  
	    final List<ColumnConfig> columns = new ArrayList<ColumnConfig>(); 
	    	    
	    ColumnConfig norecpakai = new ColumnConfig("no_rec_pakai", "No. Rec Pakai", 120);
	    columns.add(norecpakai);
	    
	    ColumnConfig tgl = new ColumnConfig("tanggal", "Tanggal", 100);
	    columns.add(tgl);
	    
	    ColumnConfig start_time = new ColumnConfig("start_time", "Start", 100);
	    columns.add(start_time);
	    
	    ColumnConfig end_time = new ColumnConfig("end_time", "End", 100);
	    columns.add(end_time);
	    
	    ColumnConfig volume_pakai = new ColumnConfig("volume_pakai", "Volume", 150);
	    columns.add(volume_pakai);	  
	    
	    ColumnConfig id_unit = new ColumnConfig("id_unit", "ID Unit", 150);
	    columns.add(id_unit);	  
	   
	    storePemakaian = new GroupingStore();  
	    storePemakaian.setMonitorChanges(true); 
	    storePemakaian.groupBy("id_unit");  
	    
	    final ColumnModel cm = new ColumnModel(columns);
	    
	    grid = new Grid(storePemakaian, cm);
	    grid.setBorders(true);  
	    
	    GroupingView view = new GroupingView();  
	    view.setShowGroupedColumn(false);  
	    view.setForceFit(true);  
	    view.setGroupRenderer(new GridGroupRenderer() {  
	      public String render(GroupColumnData data) {  
	        String f = cm.getColumnById(data.field).getHeader();  
	        String l = data.models.size() == 1 ? "Item" : "Items";  
	        return f + ": " + data.group + " (" + data.models.size() + " " + l + ")";  
	      }  
	    });  
	    
	    grid.setView(view);
	    
	    return grid;  
	  }  
	  
	  private void getList(){
		  Map map = new FastMap();
      	map.put("id_pltu", Registry.get("idPltu").toString());
      	map.put("blth", "");
      	map.put("id_unit", "");
      	
      	service.getListPemakaianBB("LIST_PEMAKAIAN", map, new AsyncCallback<List<Map>>() {
				
				@Override
				public void onSuccess(List<Map> result) {
					// TODO Auto-generated method stub
					storePemakaian.removeAll();
					storePemakaian.add(ClientModule.setListBaseModelFromListMap(result));
				}
				
				@Override
				public void onFailure(Throwable caught) {
					// TODO Auto-generated method stub
					GWT.log(caught.getMessage());
				}
			});
	  }
}
